package com.teamit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.teamit.entity.Employee;
import com.teamit.entity.Goal;
import com.teamit.entity.Task;
import com.teamit.util.db.DBHelp;
import com.teamit.util.db.RowMapper;

public class TaskDao {

	DBHelp<Task> db = new DBHelp<Task>();
	
	public void save(Task task){
		String sql = "insert into t_task(id,`name`,`desc`,state,`level`,begintime,endtime,createtime,employee_id,goal_id,rate) values(?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, task.getId(),task.getName(),task.getDesc(),task.getState(),task.getLevel(),task.getBegintime(),task.getEndtime(),task.getCreatetime(),task.getEmployeeid(),task.getGoalid(),task.getRate());
	}
	
	
	public List<Task> findByGoalId(String goalid){
		String sql = "SELECT tt.id,tt.name,tt.desc,tt.state,tt.level,tt.begintime,tt.endtime,tt.createtime,tt.employee_id,tt.goal_id,tt.rate,te.username FROM t_task AS tt LEFT JOIN t_employee AS te ON te.id = tt.employee_id WHERE goal_id=?";
		return db.executeQueryToList(sql, new TaskRowMapper(), goalid);
	}
	public Task findById(String id){
		String sql = "SELECT tt.id,tt.name,tt.desc,tt.state,tt.level,tt.begintime,tt.endtime,tt.createtime,tt.employee_id,tt.goal_id,tt.rate,te.username FROM t_task AS tt LEFT JOIN t_employee AS te ON te.id = tt.employee_id WHERE tt.id=?";
		return db.executeQueryToObject(sql, new TaskRowMapper(), id); 
	}
	
	private class TaskRowMapper implements RowMapper<Task>{

		public Task mapRow(ResultSet rs) throws SQLException {

			Task task = new Task();
			task.setId(rs.getString("id"));
			task.setName(rs.getString("name"));
			task.setDesc(rs.getString("desc"));
			task.setState(rs.getString("state"));
			task.setLevel(rs.getString("level"));
			task.setBegintime(rs.getString("begintime"));
			task.setEndtime(rs.getString("endtime"));
			task.setCreatetime(rs.getString("createtime"));
			task.setEmployeeid(rs.getString("employee_id"));
			task.setGoalid(rs.getString("goal_id"));
			task.setRate(rs.getInt("rate"));
			
			Employee employee = new Employee();
			employee.setName(rs.getString("username"));
			task.setEmployee(employee);
			
			return task;
		}
	}

	public void update(Task task) {
		String sql = "update t_task set `name`=?,`desc`=?,state=?,`level`=?,rate=?,begintime=?,endtime=?,createtime=?,employee_id=? where id=?";
		db.executeSQL(sql, task.getName(),task.getDesc(),task.getState(),task.getLevel(),task.getRate(),task.getBegintime(),task.getEndtime(),task.getCreatetime(),task.getEmployeeid(),task.getId());
	}


	public List<Task> findByEmployeeId(String employeeid) {

		String sql = "SELECT tt.id,tt.name,tt.desc,tt.state,tt.level,tt.employee_id,tt.rate,tt.createtime,tt.endtime,tt.goal_id,tt.begintime,tg.name AS goal_name FROM t_task AS tt LEFT JOIN t_goal AS tg ON tt.goal_id = tg.id WHERE tt.employee_id =?";
		return db.executeQueryToList(sql, new RowMapper<Task>(){

			public Task mapRow(ResultSet rs) throws SQLException {
				
				Task task = new Task();
				task.setId(rs.getString("id"));
				task.setName(rs.getString("name"));
				task.setDesc(rs.getString("desc"));
				task.setState(rs.getString("state"));
				task.setLevel(rs.getString("level"));
				task.setBegintime(rs.getString("begintime"));
				task.setEndtime(rs.getString("endtime"));
				task.setCreatetime(rs.getString("createtime"));
				task.setEmployeeid(rs.getString("employee_id"));
				task.setGoalid(rs.getString("goal_id"));
				task.setRate(rs.getInt("rate"));
				
				Goal goal = new Goal();
				goal.setName(rs.getString("goal_name"));
				task.setGoal(goal);
				return task;
			}
		}, employeeid);
	}

	
}
